//
//  HMVisitorTableViewController.swift
//  Weibo
//
//  Created by fuyu on 2017/7/23.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit

class HMVisitorTableViewController: UITableViewController {

        //是否登陆的标记
    var isLogin: Bool = HMUserAccountViewModel.sharedViewModel.isLogin
        //访客视图
        var visitorView: HMVisitorView?
        
        //  自定义视图loadView里面写, 重写loadView目的就是要自定义控制器的视图
        override func loadView() {
            
            if isLogin {
                super.loadView()
            } else {
                //自定义访客视图
                visitorView = HMVisitorView()
                visitorView?.delegate = self
                //  设置点击登录或者注册执行的闭包
                visitorView?.loginCallBack = { [unowned self] in
                    print(self)
                    print("闭包回调的")
                    self.entherLogin()
                }
                //  设置导航栏按钮
                setupNavUI()
                //  自定义控制器视图
                view = visitorView
            }
        }
    //  设置导航栏按钮
    private func setupNavUI() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(registerAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: #selector(loginAction))
    }
    
    //  MARK: --导航栏按钮点击事件
    @objc private func registerAction() {
        print("注册")
        entherLogin()
    }
    
    @objc private func loginAction() {
        print("登录")
        entherLogin()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    //进入微博登陆界面
    private func entherLogin() {
        
        let oauthVC = HMOAuthViewController()
        let nav = HMNavigationController(rootViewController:oauthVC)
        self.present(nav,animated: true,completion: nil)
        
        
    }
}
//  MARK: --HMVisitorViewDelegate
extension HMVisitorTableViewController: HMVisitorViewDelegate {
    func loginOperation() {
        print("我是代理对象回调过来的")
    }


}
